package bounjour;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class User extends IObjectFactory {

	private int id;
	public int getId() { return id; }

	private String displayName;
	public String getDisplayName() { return displayName; }
	public void setDisplayName(String value) { displayName = value; }

	private String email;
	public String getEmail() { return email; }
	public void setEmail(String value) { email = value; }

	private java.util.Date registerDate;
	public java.util.Date getRegisterDate() { return registerDate; }
	public void setRegisterDate(java.util.Date value) { registerDate = value; }
	
	@Override
	void buildFromReader(ResultSet RS) {
		try {
			this.id = RS.getInt("ID");
			this.registerDate = RS.getDate("RegisterDate");
			this.displayName = RS.getString("DisplayName");
			this.email = RS.getString("Email");
		} catch (Exception e) {
			// TODO: handle exception
		}
	}
	
	public void save(String password) {
		Connection cn = null;
		java.sql.PreparedStatement ps = null;
		
	    try{
	    	
	    	cn = getConnection();
	    	if (this.id == 0) { // create	    		
			    ps = cn.prepareStatement("INSERT INTO Users (DisplayName,Email,Password,RegisterDate) VALUES (?,?,?,?)", Statement.RETURN_GENERATED_KEYS);
			    ps.setString(1, this.getDisplayName());
			    ps.setString(2, this.getEmail());
			    ps.setString(3, password);
			    ps.setDate(4, new java.sql.Date(new Date().getTime()));
			    ps.execute();
			    ResultSet rs = ps.getGeneratedKeys();
			    if (rs.next()) this.id = rs.getInt(1);
			    rs.close();
	    	}
	    	else { // update
	    		ps = cn.prepareStatement("UPDATE Users SET DisplayName=?, Email=?, Password=?) WHERE ID=?");
	    		ps.setString(1, this.getDisplayName());
			    ps.setString(2, this.getEmail());
			    ps.setString(3, password);
			    ps.setInt(4, this.getId());
			    ps.execute();
	    	}
	    	cn.close();
	    	ps.close();
	    }
	    catch (Exception sqlEx){
	      System.err.println(sqlEx);
	    }			
	}
	
	static public List<User> getUsers() {
		return getObjectList(User.class, "SELECT * FROM Users");
	}
	
	static public User getUserByID(int id) {
		return getSingleObject(User.class, "SELECT * FROM Users WHERE ID=?;", id);
	}
	
	static public User login(String username, String password) {
		return getSingleObject(User.class, "SELECT * FROM Users WHERE `Email`=? AND `Password`=?;", username, password);
	}
	
	static public ArrayList<String> register(String displayName, String userEmail, String userPassword,String userPassword2){
		ArrayList <String> resultList = new ArrayList<String>();
		
		if (Util.validateEmail(userEmail)) {
			Connection cn = null;
			java.sql.PreparedStatement ps = null;
			
		    try{	    	
		    	cn = getConnection();
		    	    ps = cn.prepareStatement("SELECT * from Users WHERE `Email` = ?");
				    ps.setString(1, userEmail);
				    ResultSet rs = ps.executeQuery();
				    if (rs.next()) resultList.add("Email is already in use");
				    rs.close();
		    	    ps.close();
		    } catch (Exception sqlEx){
		      System.err.println(sqlEx);
		    }
		} else {
			resultList.add("Email is in invalid format");
		}
		// Check if passwords match
		if (!userPassword.equals(userPassword2)){
			resultList.add("Passwords do not match");
		}
		return resultList;
	}
	

}
